package com.robotlab.easytranslate.fragment;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.robotlab.easytranslate.R;

import java.text.SimpleDateFormat;


public class AboutFragment extends Fragment implements View.OnClickListener{

    private String version;
    private TextView versionText;
    private LinearLayout openSourceCode,githubFollow,weiboFollow,email,share,translateSource,
            ocrSource,onePicSource,versionLayout,author,alipay,wechatPay;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_about, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {
        wechatPay = (LinearLayout) view.findViewById(R.id.wechatPay);
        alipay = (LinearLayout) view.findViewById(R.id.alipay);
        versionLayout = (LinearLayout) view.findViewById(R.id.verisonLayout);
        author = (LinearLayout) view.findViewById(R.id.author);
        openSourceCode = (LinearLayout) view.findViewById(R.id.openSourceCode);
        githubFollow = (LinearLayout) view.findViewById(R.id.github_follow);
        weiboFollow = (LinearLayout) view.findViewById(R.id.weibo_follow);
        translateSource = (LinearLayout) view.findViewById(R.id.tranlateSource);
        ocrSource = (LinearLayout) view.findViewById(R.id.ocrSource);
        onePicSource = (LinearLayout) view.findViewById(R.id.onePicSource);
        email = (LinearLayout) view.findViewById(R.id.email);
        share = (LinearLayout) view.findViewById(R.id.share);
        versionText = (TextView)  view.findViewById(R.id.version_id);
        PackageManager manager=getActivity().getPackageManager();
        try {
            PackageInfo info=manager.getPackageInfo(getActivity().getPackageName(),0);
            versionText.setText("Version: "+info.versionName);
            version = info.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        openSourceCode.setOnClickListener(this);
        githubFollow.setOnClickListener(this);
        weiboFollow.setOnClickListener(this);
        email.setOnClickListener(this);
        share.setOnClickListener(this);
        onePicSource.setOnClickListener(this);
        translateSource.setOnClickListener(this);
        ocrSource.setOnClickListener(this);
        versionLayout.setOnClickListener(this);
        author.setOnClickListener(this);
        alipay.setOnClickListener(this);
        wechatPay.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.openSourceCode:
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://github.com/varenyzc/easytranslate"));
                startActivity(intent);
                break;
            case R.id.github_follow:
                Intent intent1 = new Intent(Intent.ACTION_VIEW);
                intent1.setData(Uri.parse("https://github.com/varenyzc"));
                startActivity(intent1);
                break;
            case R.id.weibo_follow:
                Intent intent2 = new Intent(Intent.ACTION_VIEW);
                intent2.setData(Uri.parse("https://weibo.com/varen5201314"));
                startActivity(intent2);
                break;
            case R.id.email:
                Intent intent3 = new Intent(Intent.ACTION_SENDTO);
                intent3.setData(Uri.parse("mailto:465057895@qq.com"));
                startActivity(intent3);
                break;
            case R.id.share:
                Intent share_intent = new Intent();
                share_intent.setAction(Intent.ACTION_SEND);//设置分享行为
                share_intent.setType("text/plain");//设置分享内容的类型
                share_intent.putExtra(Intent.EXTRA_SUBJECT,"我发现了一款很好用的翻译APP");//添加分享内容标题
                share_intent.putExtra(Intent.EXTRA_TEXT, "轻翻译，轻量级的翻译软件。\n下载地址：https://www.coolapk.com/apk/216403");//添加分享内容
                //创建分享的Dialog
                share_intent = Intent.createChooser(share_intent, "分享");
                startActivity(share_intent);
                break;
            case R.id.ocrSource:
                Intent intent4 = new Intent(Intent.ACTION_VIEW);
                intent4.setData(Uri.parse("http://ai.baidu.com/"));
                startActivity(intent4);
                break;
            case R.id.tranlateSource:
                Intent intent5 = new Intent(Intent.ACTION_VIEW);
                intent5.setData(Uri.parse("http://open.iciba.com/?c=api"));
                startActivity(intent5);
                break;
            case R.id.onePicSource:
                SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                String date = sDateFormat.format(new java.util.Date());
                Intent intent6 = new Intent(Intent.ACTION_VIEW);
                intent6.setData(Uri.parse("http://news.iciba.com/views/dailysentence/daily.html#!/detail/title/"+date));
                startActivity(intent6);
                break;
            case R.id.verisonLayout:
                Toast.makeText(getActivity(),"当前版本号为"+version,Toast.LENGTH_SHORT).show();
                break;
            case R.id.author:
                Intent intent8 = new Intent(Intent.ACTION_VIEW);
                intent8.setData(Uri.parse("https://varenyzc.github.io"));
                startActivity(intent8);
                Toast.makeText(getActivity(),"可能是地球上最帅的Android开发者(`・ω・´)",Toast.LENGTH_SHORT).show();
                break;
            case R.id.alipay:
                try {
                    //利用Intent打开支付宝
                    String urlCode="FKX03601L4PGG4DO312W39";
                    Intent intent7=Intent.parseUri(("intent://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&" +
                            "qrcode=https%3A%2F%2Fqr.alipay.com%2F{urlCode}%3F_s%3Dweb-other&_t=1472443966571#Intent;" +
                            "scheme=alipayqr;package=com.eg.android.AlipayGphone;end").replace("{urlCode}", urlCode),1);
                    startActivity(intent7);
                } catch (Exception e) {
                    //若无法正常跳转，在此进行错误处理
                    Toast.makeText(getActivity(), "无法跳转到支付宝，请检查您是否安装了支付宝！", Toast.LENGTH_SHORT).show();
                }break;
            case R.id.wechatPay:
                try {
                    //利用Intent打开微信
                    Uri uri = Uri.parse("wxp://f2f0Y43JA0c3RiqZpP-3kYG7_F4_Kr5JS9OM");
                    Intent intent7 = new Intent(Intent.ACTION_VIEW, uri);
                    startActivity(intent7);
                } catch (Exception e) {
                    //若无法正常跳转，在此进行错误处理
                    Toast.makeText(getActivity(), "微信支付暂未支持", Toast.LENGTH_SHORT).show();
                }break;
        }
    }
}
